//写法1，直接写
// export default (state: any, action: any) => {
//     switch (action.type) {
//         case "change2":
//             return {
//                 ...state,
//                 numberValue: action.payload
//             };
//         default:
//             return {
//                 ...state,
//                 numberValue:222
//             };
//     }
// }

//写法2,用写法1注解下面的写法2即可
import lodash from 'lodash';
import reducers from './reducers'
import produce from 'immer'

interface Reducers {
    [key: string]: (state: any, action: any) => any;
  }

export default (state: any, action: any) => {
  const reducer = (reducers as Reducers)[action.type];
   //拿到函数
    // const reducer = (state: any, action: any) => {
    //     state.numberValue = action.payload
    //     return state;
    // }
  if (lodash.isFunction(reducer)) {
    return produce(state, (draftState: any) => {
        //复制state为draftState ，传入reducer中
      return reducer(draftState, action)
    });
  }
  return state;
};
